package ezy.lite.util;

import android.content.Context;
import android.os.Environment;

import java.io.File;
import java.io.FileFilter;
import java.math.BigDecimal;

public class Cleaner {
    /** 清除本应用内部缓存(/data/data/com.xxx.xxx/cache) */
	public static void cleanInternalCache(Context context) {
        FileUtil.delete(context.getCacheDir());
	}

	/** 清除本应用所有数据库(/data/data/com.xxx.xxx/databases) */
	public static void cleanDatabases(Context context) {
        FileUtil.clean(context.getDir("databases", Context.MODE_PRIVATE), true);
	}

	/** 清除本应用SharedPreference(/data/data/com.xxx.xxx/shared_prefs) */
	public static void cleanSharedPreference(Context context) {
        FileUtil.clean(context.getDir("shared_prefs", Context.MODE_PRIVATE), true);
	}

	/** 清除/data/data/com.xxx.xxx/files下的内容 */
	public static void cleanFiles(Context context) {
		FileUtil.clean(context.getFilesDir(), true);
	}

	/** 清除外部cache下的内容(/mnt/sdcard/android/data/com.xxx.xxx/cache) */
	public static void cleanExternalCache(Context context) {
		if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            FileUtil.delete(context.getExternalCacheDir());
		}
	}

	public static void clean(String path) {
        FileUtil.clean(new File(path), false);
	}

	/** 清除本应用所有的数据 */
	public static void cleanAll(Context context, String... paths) {
        cleanWebViewCache(context);
		cleanInternalCache(context);
		cleanExternalCache(context);
		cleanDatabases(context);
		cleanSharedPreference(context);
		cleanFiles(context);
		for (String path : paths) {
            FileUtil.delete(new File(path));
		}
    }

    public static void cleanWebViewCache(Context context){
        //清理Webview缓存数据库
        try {
            context.deleteDatabase("webview.db");
            context.deleteDatabase("webviewCache.db");
        } catch (Exception e) {
            e.printStackTrace();
        }
        for (File dir : context.getCacheDir().listFiles(filter)) {
            FileUtil.delete(dir);
        }
//        for (File dir : context.getDir("./", Context.MODE_PRIVATE).listFiles(filter)) {
//            FileUtil.delete(dir);
//        }

    }
    private static FileFilter filter = new FileFilter() {
        @Override
        public boolean accept(File file) {
            return file.getPath().contains("webview");
        }
    };
	public static long getFolderSize(File file) throws Exception {
		long size = 0;
		try {
			File[] fileList = file.listFiles();
			for (int i = 0; i < fileList.length; i++) {
				// 如果下面还有文件
				if (fileList[i].isDirectory()) {
					size = size + getFolderSize(fileList[i]);
				} else {
					size = size + fileList[i].length();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return size;
	}
public static String getTotalCache(File file){
	String cache="";
	try {
		cache= getFormatSize(getFolderSize(file));
	} catch (Exception e) {
		e.printStackTrace();
	}
	return cache;
}
	/**
	 * 格式化单位
	 *
	 * @param size
	 * @return
	 */
	public static String getFormatSize(double size) {
		double kiloByte = size / 1024;
		if (kiloByte < 1) {
//            return size + "Byte";
			return "0K";
		}

		double megaByte = kiloByte / 1024;
		if (megaByte < 1) {
			BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
			return result1.setScale(2, BigDecimal.ROUND_HALF_UP)
					.toPlainString() + "KB";
		}

		double gigaByte = megaByte / 1024;
		if (gigaByte < 1) {
			BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
			return result2.setScale(2, BigDecimal.ROUND_HALF_UP)
					.toPlainString() + "MB";
		}

		double teraBytes = gigaByte / 1024;
		if (teraBytes < 1) {
			BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
			return result3.setScale(2, BigDecimal.ROUND_HALF_UP)
					.toPlainString() + "GB";
		}
		BigDecimal result4 = new BigDecimal(teraBytes);
		return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString()
				+ "TB";
	}
}